<?php
   /**
    *
    * FILE:          community_totals.php
    * LOCATION:      /root/forms/
    * REFERENCES:    none 
    * FUNCTION:          
    * DEPENDENCIES: 
    * ==============================================================================
    * 
    * @version $Id: nav.php,v 0.1 2007/09/3
    * @copyright Copyright (c) none
    * @author ChrisV
    * @license http://opensource.org/licenses/gpl-license.php GNU General 
    * Public License (GPL)
    * 
    * ==============================================================================
    * 
    */

   //connect to db
   include('includes/db_connect.php');
   
   //who is handling this record?
   $serviceRecordCoordinator = $loggedIn;
   $coordinatorID = $loggedInUserID;

	//echo $coordinator;
   	//echo $coordinatorID;
   
	
	//This will get passed from the Community Totals List page
	//so we can set the ID to retrieve and existing file
	$CT_id = $_GET['CT_id'];
	
	//this variable is the MAIN FLAG for this section
	//make sure this is set correctly. MAKE SURE ITS AN INT
	if($CT_id!='')
	{
		$CT_id= (int)$CT_id;
	}else{
		$CT_id = 0;
	}
	
	//echo "<h1>".$CT_id."</h1>";
	
	$getCTRecord = "SELECT * 
	                FROM community_totals 
					  WHERE CT_id = ".$CT_id;
	
	if ($CTRecordData = mysql_fetch_array(mysql_query($getCTRecord))){
	
		$CT_retrieved_id = $CTRecordData["CT_id"];
		//echo "<h1> ID #".$CT_retrieved_id."</h1>";
		$serviceRecordCoordinator = $CTRecordData["CT_Coordinator_Name"];
		$serviceRecordMonth = $CTRecordData["CT_Month"];
		$retrievedServiceRecordYear = $CTRecordData["CT_Year"];
		$totalServiceRecordContacts = $CTRecordData["CT_Contacts"];
		$retrievedServiceRecordHoursTotal = $CTRecordData["CT_Total_Hours"];
		echo $retrievedServiceRecordHoursTotal;
		
	}elseif($CT_id != 0){
		
		echo "<h1>Nothing was found for this Community Total Record Number:</h1>";
		echo "<h2>".$CT_id."</h2>";
		echo "<h4>Please Create a New Community Total Record.</h4>";
		die();
	}
?>

<form action="actions/ProcessCommunityTotals.php" method="post">
	<div style="border:1px #ddd solid;padding:3px;">
		<h2>Monthly Community Service Record</h2>
		<table class="table2" id="communityTotalsForm">
			<tbody>
			<tr>
				<td colspan="3">
					<h3> Community Totals Data</h3> 
					<?php
						if ($CT_id != 0){
							echo "<strong>For Record ID: ".$CT_id."</strong><br />";
						}else{
						  echo "<strong>New Record Being Created.</strong><br />";	
						}
					?>
					<p>Entered By: <?=$serviceRecordCoordinator;?></p>
					<br /><br />
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<label name="serviceRecordMonth">Service Record Month</label>
					<br>
					<select name="serviceRecordMonth" id="serviceRecordMonth" style="width: 150px;">
						<option value=""> --- </option>
						<option value="01">January</option>
						<option value="02">February</option>
						<option value="03">March</option>
						<option value="04">April</option>
						<option value="05">May</option>
						<option value="06">June</option>
						<option value="07">July</option>
						<option value="08">August</option>
						<option value="09">September</option>
						<option value="10">October</option>
						<option value="11">November</option>
						<option value="12">December</option>
					</select>
					
					<script type="text/javascript">					
						for(i=0; i<$('serviceRecordMonth').options.length; i++) {
							if($('serviceRecordMonth').options[i].value=='<? echo $serviceRecordMonth ?>') {
								$('serviceRecordMonth').options[i].selected = true;
							}
						}
					</script>
				</td>
				<td style="padding-left:180px;">
					<label name="serviceRecordYear">Service Record Year</label>
					<br>
					<select name="serviceRecordYear" id="serviceRecordYear" style="width: 150px;">
						<option value=""> --- </option>
              <?php				
						for($i=0; $i<2; $i++) {
							$serviceRecordYear = date("Y", mktime(0,0,0,1,1,date("Y")-$i));
							echo "<option value=\"".$serviceRecordYear."\">".$serviceRecordYear."</option>";
						}?>
					</select>
					<script type="text/javascript">						
						for(i=0; i<$('serviceRecordYear').options.length; i++) {
							if($('serviceRecordYear').options[i].value=='<? echo $retrievedServiceRecordYear; ?>') {
								$('serviceRecordYear').options[i].selected =true;
							}
						}
					</script>
				</td>
			</tr>
			<tr>
				<td><br></td>
			</tr>
			<tr>
				<td colspan="3">
					<!-- NESTED TABLE-->
					<table width="100%">
						<tbody>
						<tr>
							<!-- Community Total Coordinator -->
							<td>
								<label name="serviceRecordCoordinator">Coordinator</label>
								<br>
								<input type="text" style="width: 225px;" 
								        value="<?php echo $serviceRecordCoordinator;?>" 
										name="serviceRecordCoordinator" 
										id="serviceRecordCoordinator">
							</td>
							<!-- Service Contacts -->
							<td style="padding:0px 0 0 80px;">
								<label name="totalServiceRecordContacts">Total Number of Contacts</label>
								<br>
								<input type="text" style="width: 170px;" 
								       name="totalServiceRecordContacts" 
										value="<?php echo $totalServiceRecordContacts; ?>" 
										id="totalServiceRecordContacts">
							</td>
						</tr>
						<tr>
							<td><br></td>
						</tr>
						<tr>
							<td colspan="3">	
								<h3>Community Service Types</h3>
							</td>
						</tr>
						<tr>
							<td colspan="3">
								<!-- NESTED TABLE -->
								<table width="100%">
									<tbody>
										<tr class="alt_objective">
											<th colspan="2">Service</th>
											<th>Time (Hours)</th>
										</tr>
									<?php 
									
											//OKAY, we need to do some big boy sql work here
									
											//get Community Total Types from DB so we can populate form elements
											$community_total_query = "SELECT * 
											                          FROM lst_community_totals";
				
											//echo $community_total_query;
											
											$community_total_result = mysql_query($community_total_query);

											while($community_total = mysql_fetch_array($community_total_result)){
											
												//k, now we know all the possible CT Types, lets tag 'em
												$CT_categoryId = $community_total["id"];
												$CT_category = $community_total["community_total_type"];
												
												//echo $CT_categoryId;
												//echo $CT_category;
												
												//now that they are defined, lets use this data to find existing record
												//data for this id in the  ** community_total_service ** table
												
												$CT_ServiceQuery = "SELECT * 
																		FROM community_totals_service 
																		WHERE stored_CT_id = '".$CT_id."' 
																		AND CT_Service_id = '".$CT_categoryId."'";
												
												$CT_ServiceResult = mysql_query($CT_ServiceQuery);
												
												
												
												if($CT_ServiceRow = mysql_fetch_array($CT_ServiceResult)) {
												
													$serviceRecordHours = $CT_ServiceRow["CT_Service_Hours"];
													$CT_ServiceRecordData = $CT_ServiceRow["CT_Service_Description"];
												
												} else {
												
													$serviceRecordHours = 0;
													$CT_ServiceRecordData="";
												
												}
											
										
												echo "<tr class=\"odd\">";
								?>
                
															<td colspan="2">
																<input name="<?php echo $CT_categoryId ?>_category" 
																                id="<?php echo $CT_categoryId ?>_category"
																<?php if($CT_ServiceRow) echo "checked" ?>  type="checkbox" />
																<?php echo $CT_category; ?> 
															</td>
															<td>
																<input type="text" 
																        class="hour" 
																		name="<?php echo $CT_categoryId ?>_hours" value="<?php echo $serviceRecordHours ?>"
																		 onkeyup="sumInputs('hour', 'communityTotalsForm', $('serviceRecordHoursTotal')); 
																		                       updateCheckBox($('<?php echo $CT_categoryId ?>_category'))">
															</td>
													</tr>
													<tr>
														<td colspan="3">
															<label style="margin-left: 5px;">Describe Service Provided</label>
															<br /><br />
															<textarea name="<?php echo $CT_categoryId ?>_service"
																		cols="105" 
																		rows="10">
																<?php echo $CT_ServiceRecordData ?>
															</textarea>
															<br /><br />
														</td>
													</tr>
										<?php 
												
											} //end while loop to get categories, checkbox, and textarea elements
											
										?>
										<tr>
											<td><br /></td>
										</tr>
										<tr>
											<td colspan="2">
												<label name="serviceRecordHoursTotal">
													<h4 style="text-align: right;">Total Service Hours</h4>
												</label>
											</td>
											<!-- THIS IS A TEXTBOX NOW, BUT IT SHOULD JUST ECHO THE TOTALS -->
											<td>
												<input type="text" readonly
														style="width: 5em; line-height: 1.5; font-size: 1.2em; font-weight: bold; background-color:#F5F6CE; color:#DF7401;"
														name="serviceRecordHoursTotal" 
														id="serviceRecordHoursTotal"
														value="<?=$serviceRecordHoursTotal?>" />
											</td>
										</tr>
									</tbody>
								</table>
							</td>
						</tr>
						<tr>
							<td><br></td>
						</tr>
						<!-- FORM CONTROLS -->
						<tr>
						<td colspan="3" align="right">
							<input name="CT_id" type="hidden" value="<?php echo $CT_id;?>">
							<input value="Submit" class="btn" type="submit">
							<input class="btn" type="button" value="Cancel" 
							onclick="window.location='admin.php?'"/>
						</td>
					</tr>
					</tbody>
				</table>
			</div>
			<br>
			<br>
		</form>
		<script>
			sumInputs('hour', 'communityTotalsForm', $('serviceRecordHoursTotal'));
		</script>
